package com.burgeon.backend.SHOPBACKEND.service.Impl;

import com.burgeon.backend.SHOPBACKEND.entity.GoodsBanner;
import com.burgeon.backend.SHOPBACKEND.entity.ShopIndexImg;
import com.burgeon.backend.SHOPBACKEND.mappers.GoodsBannerMapper;
import com.burgeon.backend.SHOPBACKEND.service.BackendGoodsService;
import com.burgeon.backend.SHOPBACKEND.service.GoodsBannerService;
import com.burgeon.backend.SHOPBACKEND.service.ex.UploadException;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.mappers.AccountMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Service
public class GoodsBannerServiceImpl implements GoodsBannerService {
    @Autowired
    private GoodsBannerMapper goodsBannerMapper;


    /******新轮播图*****/

    @Override
    public void addGoodsBanner(MultipartFile file, String account, String id, String token) throws UploadException {

        if (file.isEmpty()) {
            System.out.println("文件为空");
            throw new UploadException("文件为空");
        }

        String types = "banner";     //将图片存到本地仓库
        String img = file.toString();
        GoodsBanner goodsBanner = new GoodsBanner();
        goodsBanner.setImg(img);
        goodsBanner.setGid(id);
        goodsBanner.setToken(account);

        goodsBannerMapper.addbanner(goodsBanner);

        String fileName = file.getOriginalFilename(); // 文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));// 后缀名
        //http://lfwdcdn.cloudsmicro.com/img/mgg-shop/2.png
        String filePath = "//root//code//img//" + account + "-" + types + "//"; // 上传后的路径
        fileName = id + suffixName; // 新文件名
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }

        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
            throw new UploadException("系统错误");
        } catch (Exception e) {
            System.err.println(e.getMessage());
            throw new UploadException("系统错误");
        }

            /*}else if (state=="2"){

                 MultipartFile multipartFile = file;

                String types = "shop";
                String img = file.toString();
                ShopIndexImg shopIndexImg = new ShopIndexImg();
                shopIndexImg.setImgIntegral(img);
                shopIndexImg.setGidIntegral(id);
                shopIndexImg.setToken(account);
                shopIndexImg.setAccount(account);
                goodsBannerMapper.addIndexImg(shopIndexImg);

                String fileName = multipartFile.getOriginalFilename(); // 文件名
                String suffixName = fileName.substring(fileName.lastIndexOf("."));// 后缀名
                //http://lfwdcdn.cloudsmicro.com/img/mgg-shop/2.png
                String filePath = "//root//code//img//" + account + "-" + types + "//goods//"; // 上传后的路径
                fileName = id + suffixName; // 新文件名
                File dest = new File(filePath + fileName);
                if (!dest.getParentFile().exists()) {
                    dest.getParentFile().mkdirs();
                }
                try {
                    multipartFile.transferTo(dest);
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new UploadException("系统错误");
                } catch (Exception e) {
                    System.err.println(e.getMessage());
                    throw new UploadException("系统错误");
                }


            }else if(state=="3"){

            MultipartFile multipartFile = file;


                *//**将图片存到本地仓库*//*
                String types = "price";
                String img = file.toString();
                ShopIndexImg shopIndexImgPrice = new ShopIndexImg();
                shopIndexImgPrice.setImgPrice(img);
                shopIndexImgPrice.setGidPrice(id);
                shopIndexImgPrice.setImgIntegral(img);
                goodsBannerMapper.addIndexImgPrice(shopIndexImgPrice);

                String fileName = multipartFile.getOriginalFilename(); // 文件名
                String suffixName = fileName.substring(fileName.lastIndexOf("."));// 后缀名
                //http://lfwdcdn.cloudsmicro.com/img/mgg-shop/2.png
                String filePath = "//root//code//img//" + account + "-" + types + "//goods//"; // 上传后的路径
                fileName = id + suffixName; // 新文件名
                File dest = new File(filePath + fileName);
                if (!dest.getParentFile().exists()) {
                    dest.getParentFile().mkdirs();
                }
                try {
                    multipartFile.transferTo(dest);
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new UploadException("系统错误");
                } catch (Exception e) {
                    System.err.println(e.getMessage());
                    throw new UploadException("系统错误");
                }

        }
*/


    }


}
